package com.xyh.transaction.controller;

import com.xyh.transaction.entity.dto.cart.CartDto;
import com.xyh.transaction.entity.dto.user.ArrDto;
import com.xyh.transaction.entity.vo.CartVo;
import com.xyh.transaction.response.ResponseResult;
import com.xyh.transaction.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Api(tags = "购物车管理")
@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation(value = "合并购物车", notes = "合并当前登录用户购物车")
    @PostMapping("merge")
    public ResponseResult<Void> merge(@RequestBody List<CartDto> cartDtoList) {
        cartService.mergeCart(cartDtoList);
        return ResponseResult.success("查询成功！", null);
    }

    @ApiOperation(value = "获取购物车列表", notes = "获取当前用户购物车数据")
    @GetMapping
    public ResponseResult<List<CartVo>> cartList() {
        List<CartVo> cartList = cartService.getCartList();
        return ResponseResult.success("查询成功", cartList);
    }

    @ApiOperation(value = "添加购物车", notes = "把数据存入数据库")
    @PostMapping
    public ResponseResult<Void> addCart(@RequestBody CartDto addCartDto) {
        cartService.addCart(addCartDto);
        return ResponseResult.success("操作成功！", null);

    }

    @ApiOperation(value = "更新购物车商品", notes = "更新购物车商品")
    @PostMapping("update")
    public ResponseResult<Void> updateSelected(@RequestBody List<CartDto> cartDtos) {
        cartService.updateCartBatch(cartDtos);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "删除接口", notes = "根据id删除购物车商品")
    @DeleteMapping
    public ResponseResult<Void> deleteCart(@RequestBody ArrDto arrDto) {
        cartService.deleteCart(Arrays.asList(arrDto.getId()));
        return ResponseResult.success("操作成功！", null);
    }


}
